package com.qidian.k12.aspect;

import com.qidian.k12.util.ObjectTools;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 戴着假发的程序
 */
//@Component
//@Aspect
public class K12Aspect {

    private List<Long> times = new ArrayList<>();

    @Pointcut("execution(* com.qidian.k12.service.TemplateService.queryById(..))")
    public void templateQueryById(){};

    @Around("templateQueryById()")
    public Object testTime(ProceedingJoinPoint joinPoint){
        Object returnVal = null;
        long time1 = System.currentTimeMillis();
        try {
            returnVal = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        long time = (time2-time1);
        times.add(time);// 修改集合
//        System.out.println(times.size()+"--"+time);
        if(times.size()>=5000){
            synchronized (this) {
                Long max = Collections.max(times);
                System.out.println("5000次测试的访问耗时为：" + max);
                times.clear();
            }
        }
        return returnVal;
    }

}
